package com.zysc.processflow.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysc.processflow.common.base.ResultBody;
import com.zysc.processflow.common.util.AesUtil;
import com.zysc.processflow.entity.Person;
import com.zysc.processflow.service.IPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import com.zysc.processflow.common.base.BaseController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 人员表 前端控制器
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2022-11-23
 */
@Api(value = "人员信息", tags = "人员信息")
@RestController
@RequestMapping("/person")
public class PersonController extends BaseController<Person> {
    private static Logger logger = LoggerFactory.getLogger(PersonController.class);
    @Autowired
    IPersonService service;

    public PersonController(IPersonService service) {
        super(service);
    }

    @ApiOperation("获取人员列表(支持分页)")
    @GetMapping(value = "GetPersonList")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "pageIndex", name = "pageIndex", dataType = "String"),
            @ApiImplicitParam(value = "pageSize", name = "pageSize", dataType = "String"),
            @ApiImplicitParam(value = "名称", name = "name", dataType = "String"),
            @ApiImplicitParam(value = "组织机构id", name = "orgid", dataType = "String"),
            @ApiImplicitParam(value = "组织机构", name = "orgname", dataType = "String"),
            @ApiImplicitParam(value = "当前组织机构id", name = "currentorgid", dataType = "String"),
    })
    public ResultBody GetPersonList(Integer pageIndex , Integer pageSize , String name ,String orgid ,String currentorgid,String currentorgtype,String orgname) {
        if (pageIndex != null && pageSize != null) {
            IPage<Person> page = new Page<>(pageIndex, pageSize);
            List<Person> res = service.GetPersonList(page, name,orgid,currentorgid,currentorgtype,orgname);
            return ResultBody.Success(res, page.getTotal());
        } else {
            List<Person> res = service.GetPersonList(null, name,orgid,currentorgid,currentorgtype,orgname);
            return ResultBody.Success(res);
        }
    }

    @ApiOperation("添加/更新人员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "人员信息", name = "entity", dataType = "Person")
    })
    @Transactional
    @PostMapping(value = "SaveOrUpdate")
    public ResultBody SaveOrUpdate(@RequestBody Person entity) {
        try{
            Boolean b = service.saveOrUpdatePersonInfo(entity);
            if(!b){
                throw new RuntimeException("保存人员信息失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error(e.getMessage(), 500);
        }
        return ResultBody.Success(entity);
    }

    @ApiOperation("删除用户")
    @PostMapping("DeletePerson")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "人员实体", name = "entity", dataType = "Person")
    })
    public ResultBody DeletePerson(@RequestBody Person entity) {
        try{
            Boolean b = service.deletePersonInfo(entity);
            if(!b){
                throw new RuntimeException("删除人员信息失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error(e.getMessage(), 500);
        }
        return ResultBody.Success(entity);
    }

    @ApiOperation("获取人员列表(支持分页)")
    @GetMapping(value = "GetzfPersonList")
    public ResultBody GetzfPersonList() {
        Map<String,String> districtZFIDmap = new HashMap() {{
            put("330800", "2031");//衢州市应急管理局
            put("330802", "2077");//柯城区应急管理局
            put("330803", "2088");//衢江区应急管理局
            put("330822", "2064");//常山县应急管理局
            put("330824", "2101");//开化县应急管理局
            put("330825", "2052");//龙游县应急管理局
            put("330881", "2018");//江山市应急管理局
            put("330851", "2114");//智造新城应急管理局
        }};
        Map<String,String> parkZFIDmap = new HashMap() {{
            put("330800", "");//衢州市园区
            put("330802", "");//柯城区园区
            put("330803", "2115");//衢江区园区
            put("330822", "2116");//常山县园区
            put("330824", "2119");//开化县园区
            put("330825", "2117");//龙游县园区
            put("330881", "2118");//江山市园区
            put("330851", "2114");//智造新城园区
        }};
        districtZFIDmap.forEach((districtcode,districtInfo)->{
            List<Person> processflowZfdutypersonReceivealarm = service.getPersonListByOrgIdAndRoleId(districtInfo, "processflow_zfdutyperson_receivealarm");
            List<Person> processflowZfdutyperson2 = service.getPersonListByOrgIdAndRoleId(districtInfo, "processflow_zfdutyperson_dailypaper");
            processflowZfdutypersonReceivealarm.forEach(person->{
                logger.info("政府"+districtInfo+"下的短信接收人员姓名"+person.getName()+" 电话:"+person.getPhone()+" id:"+person.getId());
            });
            processflowZfdutyperson2.forEach(person->{
                logger.info("政府"+districtInfo+"下的日报接收人员:"+person.getName()+" 电话:"+person.getPhone()+" id:"+person.getId());
            });
        });
        parkZFIDmap.forEach((districtcode,districtInfo)->{
            List<Person> processflowZfdutypersonReceivealarm = service.getPersonListByOrgIdAndRoleId(districtInfo, "processflow_zfdutyperson_receivealarm");
            List<Person> processflowZfdutyperson2 = service.getPersonListByOrgIdAndRoleId(districtInfo, "processflow_zfdutyperson_dailypaper");
            processflowZfdutypersonReceivealarm.forEach(person->{
                logger.info("园区"+districtInfo+"下的短信接收人员:"+person.getName()+" 电话:"+person.getPhone()+" id:"+person.getId());
            });
            processflowZfdutyperson2.forEach(person->{
                logger.info("园区"+districtInfo+"下的日报接收人员:"+person.getName()+" 电话:"+person.getPhone()+" id:"+person.getId());
            });
        });
        return null;
    }



    @ApiOperation("解密用户中心推的字符串")
    @PostMapping("jiemi")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "要解密的字符串", name = "text", dataType = "String")
    })
    public ResultBody jiemi(@RequestBody Map entity) {
        String before = (String)entity.get("text");
        logger.info("传入的要解密的字符串:"+before);
        String after = before;
        try{
            after = AesUtil.decryptCrypto(after);
            if(after==null){
                after = before;
            }
        } catch (Exception e) {
            return ResultBody.error(e.getMessage(), 500);
        }
        return ResultBody.Success(after);
    }
}
